package com.jwgf.smarowindingmachine_unity.ui;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwgf.sdcard.SDCardManager;
import com.jwgf.sdcard.StorageInterface;
import com.jwgf.sdcard.StorageInterface.SDMsgRecvListener;
import com.jwgf.smarowindingmachine_unity.R;

public class EquipmentMaintenanceErrorReportFragment  extends BaseFragment implements
		OnClickListener, SDMsgRecvListener {
	public static final String fragmentTag = "EquipmentMaintenanceErrorReportFragment";
	Button btn_error_sd;
	Button btn_error_usb;
	ImageView error_state_icon;
	TextView error_report_text;
	TextView download_msg;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		error_report_text = (TextView) getActivity().findViewById(
				R.id.error_report_text);
		download_msg = (TextView) getActivity().findViewById(R.id.download_msg);
		btn_error_sd = (Button) getActivity().findViewById(R.id.btn_error_sd);
		btn_error_usb = (Button) getActivity().findViewById(R.id.btn_error_usb);

		btn_error_sd.setOnClickListener(this);
		btn_error_usb.setOnClickListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(
				R.layout.fragment_equipment_maintenance_error_report,
				container, false);
		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_error_usb:
			SDCardManager.getInstance().registerSDMsgRecvListener(this);
			SDCardManager.getInstance().checkDevice(StorageInterface.Dev.USB);
			break;
		case R.id.btn_error_sd:
			SDCardManager.getInstance().registerSDMsgRecvListener(this);
			SDCardManager.getInstance().checkDevice(StorageInterface.Dev.SD);
			break;
		}
	}

	@Override
	public void OnSDMsgRecv(int dev, int op, Bundle bundle) {
		switch (op) {
		case StorageInterface.Operation.CHECK_DEV:
			String rootPath = SDCardManager.getInstance().getRootPath(bundle);
			if (rootPath != null) {
				SDCardManager.getInstance().dumpLog(dev);
				download_msg.setText(R.string.in_download);
				download_msg.setVisibility(View.VISIBLE);
			} else {
				download_msg.setText(R.string.store_none);
				download_msg.setVisibility(View.VISIBLE);
			}
			break;
		case StorageInterface.Operation.DUMP_LOG:
			download_msg.setText(R.string.complete);
			break;
		}

	}

}
